<template>
  <view>
    <view class="input-search">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
    	<button @click="search">搜索</button>
    </view>
	<uni-table ref="table" type="selection" :loading="loading" emptyText="暂无更多数据" filter-type="search"  @selection-change="selectionChange">
		<uni-tr>
			<uni-th class="th-class">
				<button class="close_btn del_btn" @click="toDelete">删除</button>
				<button class="close_btn" @click="toSyn">同步</button>
				<button class="close_btn unsyn_btn" @click="toUnSyn">撤销同步</button>
				<button class="close_btn ok_btn" @click="toEdit" v-show="order_type==5?true:false">编辑</button>
			</uni-th>
		</uni-tr>
		<uni-tr v-for="(item, index) in card_arr" :key="index">
			<uni-td width="100%" align="left" class="td-class" >
				<div @click="selectes(item)" @longpress="longPressDelete(index)" class="td_div">
					<view class="td_list">
						<text class="label">单号：</text>
						<text class="text">{{ item.FBillNo }}</text>
					</view>
					<view class="td_list">
						<text class="label">日期：</text>
						<text class="text">{{ item.FDate }}</text>
					</view>
					<view class="td_list">
						<text class="label">所在仓库：</text>
						<text class="text">{{ $Func.getStockNameByStockID(item.stock_id) }}</text>
					</view>
					<view class="td_list" v-if="item.to_stock_id">
						<text class="label">目标仓库：</text>
						<text class="text">{{ $Func.getStockNameByStockID(item.to_stock_id) }}</text>
					</view>
					<view class="td_list" v-show="item.source_code!=undefined?true:false">
						<text class="label">源单单号：</text>
						<text class="text">{{ item.source_code }}</text>
					</view>
					<view class="td_list">
						<text class="label">操作数量：</text>
						<text class="text">{{ item.total_qty }}</text>
					</view>
					<view class="td_list">
						<text class="label">状态：</text>
						<text class="text font-red">{{item.syn_status==1?'已同步':'未同步'}}</text>
					</view>
					<!-- <view class="td_list" v-show="order_type==5?true:false">
						<text class="label">流水号：</text>
						<text class="text" style="word-break: break-all;">{{ item.rd_name }}</text>
					</view> -->
					<view class="td_list">
						<text class="label">备注：</text>
						<text class="text" style="word-break: break-all;">{{ item.a_remark }}</text>
					</view>
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput
} from "@dcloudio/uni-ui"
export default {
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
      selectedIndexs:[],//选中
	  
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	borderColor: '#999'
	  },
	  keyword:'',
	  where:' 1=1 ',
	  order_type:'',
	  FBillNoPda:'',//源单编码
	  user:uni.getStorageSync('user')
    }
  },
  components: {
    uniPagination,
    uniIcons, //uni-ui的icons组件
    uniTable,
    uniTr,
    uniTh,
    uniTd,
	uniEasyinput
  },
  onPullDownRefresh() {
  	this.search()
  	uni.stopPullDownRefresh();
  },
  methods: {
	// 搜索
	search() {
		this.currentPage = 1
		this.getlist();
	},
	longPressDelete(index){
		//长按 删除
		this.selectedIndexs = []
		this.selectedIndexs.push(index)
		this.toDelete()
	},
    //获取列表
    getlist() {
		if(this.order_type == ''){
			this.$Api.msg('单据类型错误','error')
			return
		}
		let order_type = this.order_type
		if(this.order_type == 17){
			//上架（源单上架、直接上架）
			order_type = '1,17'
		}
		if(this.order_type == 18){
			//领料（拣货领料、直接领料）
			order_type = '3,18'
		}
		
		uni.showLoading({mask:true});
		this.where = " order_type in ("+order_type+") and userid='"+this.user.userid+"' and stock_id="+this.user.stock_id
		if(this.keyword!=''){
			this.where += "  and (source_code like '%"+this.keyword+"%' or FBillNo like '%"+this.keyword+"%' or factory_name like '%"+this.keyword+"%') "
		}
		if(this.FBillNoPda!=''){
			this.where += " and source_code like '%"+this.FBillNoPda+"%'"
		}
		
		this.$Db.queryCount('stockbill',this.where).then(res => {
			this.total = res[0].num;//获得总页数
			if(!this.total){
				uni.hideLoading()
			}
		})
		this.$Db.queryDataList('stockbill',this.currentPage,this.pageSize,this.where,'id','desc').then(res => {
			this.card_arr = res;
			uni.hideLoading();
		})
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    },
	// 多选
	selectionChange(e) {
		this.selectedIndexs = e.detail.index
	},
	successToList(){
		let _this = this
		this.$Api.msg('操作成功','success')
		setTimeout(()=>{
			_this.getlist()
			_this.$refs.table.clearSelection()
		},1000)
	},
	toDelete() {
		let _this = this;
		if(this.toCheck(true,true,false)){
			let bill = this.card_arr[this.selectedIndexs[0]]
			
			uni.showModal({
			    title: '确定要删除记录?',
			    success: function (res) {
			        if (res.confirm) {
						//删除本地记录、恢复源单正常状态
						_this.$Db.deleteTableData('stockbill','FBillNo',bill.FBillNo)
						_this.$Db.deleteTableData('stockbillentry','FBillNo',bill.FBillNo)
						_this.successToList()
			        }
			    }
			})
		}
	},
	toCheck(single = true,syn = false,nosyn = false){
		if(single){
			if(!this.selectedIndexs.length||this.selectedIndexs.length>1){
				this.$Api.msg('请选择单条记录进行删除');
				return
			}
		}
		
		let bill = this.card_arr[this.selectedIndexs[0]]
		if(syn){
			if(bill.syn_status){
				this.$Api.msg('已同步单据，无法操作');
				return 
			}
		}
		if(nosyn){
			if(!bill.syn_status){
				this.$Api.msg('该记录未同步,无法操作');
				return
			}
		}
		return true
	},
	toEdit(){
		let _this = this;
		if(this.toCheck(true,true,false)){
			let bill = this.card_arr[this.selectedIndexs[0]]
			bill.pcode = bill.source_code
			bill.remark = bill.a_remark
			bill.qrNoStr = bill.rd_name
			let url = ''
			//采购相关
			if(bill.order_type == 0 || bill.order_type == 14){
				uni.navigateBack({
					delta: 1,
					success: () => {
					  uni.$emit("getPlan", bill,'edit')
					}
				});
			}
			//出库相关
			if(bill.order_type == 2 || bill.order_type == 5){
				uni.navigateBack({
					delta: 1,
					success: () => {
					  uni.$emit("getAppmaterial", bill,'edit')
					}
				});
			}
		}
	},
	toSyn(){
		//回传数据
		let _this = this;
		if(this.toCheck(true,true,false)){
			let bill = this.card_arr[this.selectedIndexs[0]]
			uni.showModal({
				title: '确定要同步记录?',
				success: function (res) {
					if (res.confirm) {
						_this.$Db.backSynData('stockbill',bill,bill.order_type).then(res=>{
							_this.successToList()
						}).catch(err=>{
							_this.$Api.msg(err)
						})
					}
				},
			})
		}
	},
	toUnSyn(){
		//撤销同步，针对同步失败的单据
		let _this = this;
		if(this.toCheck(true,false,true)){
			let bill = this.card_arr[this.selectedIndexs[0]]
			uni.showModal({
				title: '确定要撤销同步记录?',
				success: function (res) {
					if (res.confirm) {
						_this.$Db.unSynData('stockbill',bill.FBillNo).then(res=>{
							_this.$Db.updateSqlData('stockbill',{syn_status:0},'FBillNo',bill.FBillNo)
							_this.successToList()
						}).catch(err=>{
							_this.$Api.msg(err)
						})
					}
				},
			})
		}
	},
	selectes(item) {
	  if (item == '') return
	  uni.navigateTo({
	  	url:'/pages/stockbill/item?FBillNo='+item.FBillNo+'&order_type='+this.order_type,
	  		delta:1
	  })
	},
  },
  mounted(e) {},
  onLoad(e) {
	this.order_type = e.order_type
	if((e.order_type==1||e.order_type==3)&&e.FBillNoPda!=undefined){
		this.FBillNoPda = e.FBillNoPda
	}
	this.getlist()
	this.$Func.setBarTitle('本地单据')
  }
}
</script>
<style lang="less">
</style>